//
//  FindViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "FindViewController.h"
#import "AfNetWorkingHandler.h"
#import "FindsModel.h"
#import "FindDetailCell.h"
#import "FindDetailModel.h"
#import <UIImageView+WebCache.h>
#import "FindPushViewController.h"
#import <MJRefresh.h>
#import "BlackManager.h"
#import "SetBlack.h"
#import "MBProgressHUDManager.h"//加载时的小菊花
@interface FindViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *findArr;
@end

@implementation FindViewController
- (void)dealloc
{
    [_path release];
    [_titleName release];
//    [_tableView release];
    [_findArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    self.navigationItem.title = _titleName;
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-newlisticon06"] style:UIBarButtonItemStylePlain target:self action:@selector(fanhui)]autorelease];
    [_tableView registerClass:[FindDetailCell class] forCellReuseIdentifier:@"Findcell"];
    [self.view addSubview:_tableView];
    //数据处理
    [self dataHandler];
    //下拉刷新
    //话题页下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [_tableView.mj_header beginRefreshing];
    [_tableView release];
    
}
//返回方法
-(void)fanhui
{
    [self.navigationController popViewControllerAnimated:YES];
}
//数据处理
-(void)dataHandler
{
    //添加加载小菊花
    [MBProgressHUDManager showHUDtoView:self.view string:@"努力加载中"];
    [AfNetWorkingHandler GetWithStr:_path complection:^(id result) {
        //
        NSArray *arr = result[@"data"][@"posts"];
        self.findArr = [FindDetailModel modelHanderWithArray:arr];
        [_tableView reloadData];
    }];
    [_tableView.mj_header endRefreshing];
    [MBProgressHUDManager removeHUD];
}
//点击进入详情webView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //push过去时隐藏tabbar
    self.hidesBottomBarWhenPushed = YES;
    FindPushViewController *detailVC = [[FindPushViewController alloc]init];
    FindDetailModel *m = _findArr[indexPath.row];
    detailVC.pathstr = m.weburl;
    [self.navigationController pushViewController:detailVC animated:YES];
    //pop回来时显示tabbar
    self.hidesBottomBarWhenPushed = NO;
    [detailVC release];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailModel *m = _findArr[indexPath.row];
    CGFloat h = [FindDetailCell heightWithText:m.content];
    CGFloat pictureH = [FindDetailCell heightWithImage:m.width WithHeight:m.height];
    return h + pictureH  + 100;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _findArr.count;
}
//页面出现之前更新
-(void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
}
-(void)setLightDayModel
{
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
-(void)setNightDayModel
{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Findcell" forIndexPath:indexPath];
    [SetBlack setCellColorWithCell:cell];
    [SetBlack setLableTitleColorWithArr:@[cell.contentLable]];
    FindDetailModel *m = _findArr[indexPath.row];
    cell.pictureStr = m.autherImg;
    [cell.userImg sd_setImageWithURL:[NSURL URLWithString:m.autherImg]];
    cell.userLable.text = m.autherName;
    cell.timeLable.text = m.date;
    cell.contentLable.text = m.content;
    //传值计算自定义高度
    cell.content = m.content;
    cell.width = m.width;
    cell.height = m.height;
    
    if (m.imgPath) {
        cell.isHave = YES;
    }else{
        cell.isHave = NO;
    }
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:m.imgPath]];
    cell.hotLabel.text = m.hot_num;
    cell.likeLable.text = m.like_num;
    cell.messageLable.text = m.comment_count;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
